const DrawUser=wx.cloud.database().collection('DrawUser')
const CreditRecord=wx.cloud.database().collection('CreditRecord')
const DrawRecord=wx.cloud.database().collection('DrawRecord')

const app=getApp()
const _ = wx.cloud.database().command

Page({
    data: {
        usercredit:0,
        userticket:0,
    },


      //网上找的随机数生产器,要用就this.rand(number),number为想要的最大数字
  rand: function (number) {
    var today = new Date();
    var seed = today.getTime();
    seed = (seed * 9301 + 49297) % 233280; //为何使用这三个数?我也不知道
    seed = seed / (233280.0)
    return Math.ceil(seed * number);
  },


    draw(){
        var date=new Date()
        let that=this
        if(!app.globalData.ischecked){
          wx.showModal({
            title:'提示',
            content:'请先登录',
            showCancel:false,
            confirmText:"好的",
            cancelColor: 'cancelColor',
            confirmColor:'#00BFFF',
            success:function(res){
              if(res.confirm){}
            }
            
          })
          
        }

      else{
        var t=wx.getStorageSync('userticket')
        if(t<=0){
          wx.showModal({
            title:'提示',
            content:'不够票了',
            showCancel:false,
            confirmText:"好的",
            cancelColor: 'cancelColor',
            confirmColor:'#00BFFF',
            success:function(res){
              if(res.confirm){}
            }
            
          })

        }
        else{
        var prizenum=this.rand(100)
        if(prizenum==1){
            DrawRecord.add({
            
                data:{
                    prize:"一等奖可乐",                 
                    createdAt:new Date(),        
                    date:String(date.getFullYear())+"/"+String(date.getMonth())+"/"+date.getDate(),

                }
            }).then(
                wx.showModal({
                    title:'中奖提醒',
                    content:'恭喜你中了一等奖可乐',
                    showCancel:false,
                    confirmText:"好的",
                    cancelColor: 'cancelColor',
                    confirmColor:'#00BFFF',
                    success:function(res){
                      if(res.confirm){}
                    }
                    
                  })
            )
        }

        else if(prizenum==2){
            DrawRecord.add({
            
                data:{
                    prize:"二等奖雪碧",                 //等等写
                    createdAt:new Date(),        //时间
                    date:String(date.getFullYear())+"/"+String(date.getMonth())+"/"+date.getDate(),
                
    
                }
            }).then(
                wx.showModal({
                    title:'中奖提醒',
                    content:'恭喜你中了二等奖雪碧',
                    showCancel:false,
                    confirmText:"好的",
                    cancelColor: 'cancelColor',
                    confirmColor:'#00BFFF',
                    success:function(res){
                      if(res.confirm){}
                    }
                    
                  })
            )
        }

        else{
            DrawRecord.add({
            
                data:{
                    prize:"无",                 //等等写
                    createdAt:new Date(),        //时间
                    date:String(date.getFullYear())+"/"+String(date.getMonth())+"/"+date.getDate(),
                
    
                }
            }).then(
                wx.showModal({
                    title:'非酋提醒',
                    content:'谢谢惠顾',
                    showCancel:false,
                    confirmText:"好的",
                    cancelColor: 'cancelColor',
                    confirmColor:'#00BFFF',
                    success:function(res){
                      if(res.confirm){}
                    }
                    
                  })
            )

        }


        
        DrawUser.where({_openid:app.globalData.userOpenId}).update({
            data:{
              ticket:_.inc(-1)
            },
            success: function(res) {
              DrawUser.where({_openid:app.globalData.userOpenId}).get().then(res=>{
                console.log(res.data)
                that.setData({              
                    //dataob:res.data,
                    usercredit:res.data[0].credit,
                    userticket:res.data[0].ticket,

                })
                console.log(that.data.usercredit)
                wx.setStorageSync('usercredit', res.data[0].credit)
                wx.setStorageSync('userticket', res.data[0].ticket)
            })
            },
          })
        }
      }
    },
    
    trade300(){
      let that=this
        if(!app.globalData.ischecked){
            wx.showModal({
              title:'提示',
              content:'请先登录',
              showCancel:false,
              confirmText:"好的",
              cancelColor: 'cancelColor',
              confirmColor:'#00BFFF',
              success:function(res){
                if(res.confirm){}
              }
              
            })
            
          }

        else{
          var c=wx.getStorageSync('usercredit')

          if(c<300){
            wx.showModal({
              title:'提示',
              content:'积分不足，无法兑换',
              showCancel:false,
              confirmText:"好的",
              cancelColor: 'cancelColor',
              confirmColor:'#00BFFF',
              success:function(res){
                if(res.confirm){}
              }
              
            })
          }
          else{
        console.log(app.globalData.ischecked)
        var date=new Date()
        CreditRecord.add({
            data:{
                creditChange:-300,
                reason:"trade300",
                createdAt:new Date(),
                date:String(date.getFullYear())+"/"+String(date.getMonth())+"/"+date.getDate(),
            
            }
        }).then(res=>{console.log(res)})
        
        DrawUser.where({_openid:app.globalData.userOpenId}).update({
            data:{
              ticket:_.inc(1),
              credit:_.inc(-300),
            },
            success: function(res) {
                wx.showModal({
                    title:'',
                    content:'兑换成功',
                    showCancel:false,
                    confirmText:"好的",
                    cancelColor: 'cancelColor',
                    confirmColor:'#00BFFF',
                    success:function(res){
                      if(res.confirm){}
                    }
                    
                  })
              DrawUser.where({_openid:app.globalData.userOpenId}).get().then(res=>{
                console.log(res.data)
                console.log("兑换票成功")
                that.setData({              
                    //dataob:res.data,
                    usercredit:res.data[0].credit,
                    userticket:res.data[0].ticket,

                })
                console.log(that.data.usercredit)
                wx.setStorageSync('usercredit', res.data[0].credit)
                wx.setStorageSync('userticket', res.data[0].ticket)
            })
            },
          })

        }

      }
    },


    trade500(){
        if(!app.globalData.ischecked){
            wx.showModal({
              title:'提示',
              content:'请先登录',
              showCancel:false,
              confirmText:"好的",
              cancelColor: 'cancelColor',
              confirmColor:'#00BFFF',
              success:function(res){
                if(res.confirm){}
              }
              
            })
            
          }

        else{
          var c=wx.getStorageSync('usercredit')
          if(c<500){
            wx.showModal({
              title:'提示',
              content:'积分不足，无法兑换',
              showCancel:false,
              confirmText:"好的",
              cancelColor: 'cancelColor',
              confirmColor:'#00BFFF',
              success:function(res){
                if(res.confirm){}
              }
            })
          }
          else{
        console.log(app.globalData.ischecked)
        var date=new Date()
        CreditRecord.add({
            data:{
                creditChange:-500,
                reason:"trade500",
                createdAt:new Date(),
                date:String(date.getFullYear())+"/"+String(date.getMonth())+"/"+date.getDate(),
            
            }
        }).then(res=>{console.log(res)})
        let that=this
        DrawUser.where({_openid:app.globalData.userOpenId}).update({
            data:{
              ticket:_.inc(2),
              credit:_.inc(-500),
            },
            success: function(res) {
                wx.showModal({
                    title:'',
                    content:'兑换成功',
                    showCancel:false,
                    confirmText:"好的",
                    cancelColor: 'cancelColor',
                    confirmColor:'#00BFFF',
                    success:function(res){
                      if(res.confirm){}
                    }
                    
                  })
              DrawUser.where({_openid:app.globalData.userOpenId}).get().then(res=>{
                console.log(res.data)
                console.log("兑换票成功")
                that.setData({              
                    //dataob:res.data,
                    usercredit:res.data[0].credit,
                    userticket:res.data[0].ticket,

                })
                console.log(that.data.usercredit)
                wx.setStorageSync('usercredit', res.data[0].credit)
                wx.setStorageSync('userticket', res.data[0].ticket)
            })
            },
          })

        }
      }
    },

    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(){
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        app.globalData.ischecked=wx.getStorageSync('ischecked')
        this.setData({
          ischecked:wx.getStorageSync('ischecked')
        })
        app.globalData.userOpenId = wx.getStorageSync('app.globalData.userOpenId')

        console.log(app.globalData.ischecked)
        console.log(app.globalData.userOpenId)
        DrawUser.where({_openid:app.globalData.userOpenId}).get().then(res=>{
            console.log(res.data)
            console.log("用户消息已经获取成功")
            this.setData({              
                //dataob:res.data,
                usercredit:res.data[0].credit,
                userticket:res.data[0].ticket,
            })
            //
            wx.setStorageSync('usercredit', res.data[0].credit)
            wx.setStorageSync('userticket', res.data[0].ticket)
        })
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})